/*
 * @(#)Object2XML.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.xmlConvert;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Object2XML
 * 
 * @author BIS 2012-12-24
 */
public class Object2XML
{

    /**
     * object2XML
     * @Description:
     * @param lstObj
     * @param outFileName
     * @throws FileNotFoundException
     */
    public static void object2XML(List lstObj, String outFileName) throws FileNotFoundException
    {

        // 构造输出XML文件的字节输出流
        File outFile = new File(outFileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));

        // 构造一个XML编码器
        XMLEncoder xmlEncoder = new XMLEncoder(bos);

        for (int i = 0; i < lstObj.size(); i++)
        {
            // 使用XML编码器写对象
            xmlEncoder.writeObject(lstObj.get(i));
        }

        // 关闭编码器
        xmlEncoder.close();
    }

    /**
     * xml2Object
     * @Description:
     * @param inFileName
     * @return
     * @throws FileNotFoundException
     */
    public static Object xml2Object(String inFileName) throws FileNotFoundException
    {
        // 构造输入的XML文件的字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFileName));
        // 构造一个XML解码器
        XMLDecoder xmlDecoder = new XMLDecoder(bis);
        // 使用XML解码器读对象
        Object obj = xmlDecoder.readObject();
        // 关闭解码器
        xmlDecoder.close();

        return obj;
    }
}
